package com.longteng.java.web.dataproducer;

import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;

import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * Created by tianqing.wang
 * DATE : 2018/3/17
 * TIME : 下午10:05
 * PROJECT : java-lesssion
 * PACKAGE : com.gitee.longteng.lession.lesson2.dataproducer
 *
 * @author <a href="mailto:celeskyking@163.com">tianqing.wang</a>
 */
public class NameDataGenerator implements DataGenerator<String> {


    private static final String FIRST_NAMES_FILE = "firstname_prefix.txt";


    private static final String LAST_NAMES_FILE = "lastname.txt";

    private List<String> firstNames = Lists.newArrayList();


    private List<String> lastNames = Lists.newArrayList();


    public NameDataGenerator(){
        try{
            URL url = Resources.getResource(FIRST_NAMES_FILE);
            List<String> result = Resources.asCharSource(url, StandardCharsets.UTF_8).readLines();
            if(result!=null){
                for(String line:result){
                    for(char c : line.toCharArray()){
                        firstNames.add(Character.toString(c));
                    }
                }
            }
            List<String> lastNameLines= Resources.asCharSource(Resources.getResource(LAST_NAMES_FILE),StandardCharsets.UTF_8).readLines();
            for(String line : lastNameLines){
                lastNames.addAll(Splitter.on("、").trimResults().omitEmptyStrings().splitToList(line));
            }
        }catch (Exception e){
            throw new IllegalArgumentException(e);
        }
    }

    @Override
    public String generate() {
        return firstNames.get(RandomUtil.getRandom(0,firstNames.size()-1))+lastNames.get(RandomUtil.getRandom(0,lastNames.size()-1));
    }

    public static void main(String[] args) {
        NameDataGenerator nameDataGenerator = new NameDataGenerator();
        for(int i=0;i<100;i++){
            System.out.println(nameDataGenerator.generate());
        }
    }
}
